//product model class definition
package model
{
	import flash.data.SQLStatement;
	
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;
	
	
	public class Product
	{
		private var _id:String;
		private var _pk:String;
		private var _description:String;
		private var _unit_cost:Number;
		private var _uom:String;
		private var _lead_time:uint;
		private var _available_quantity:int;
		private var _reorder_quantity:int;
		private var _date_created:String;
		private var _date_modified:String;
		private var _category_pk:String;
		private var _s_columns:String = "";
		private var _s_values:String;
		private var _s_insert_statement:String;
		private var _s_delete_statement:String;
		private var _a_parameters:Array;
		private var df_now:DateFormatter = new DateFormatter();
		
			
		
		public function Product()
		{
			df_now.formatString = "YYYYMMDDHHNNSS";
			_a_parameters = new Array();
		}
		
		/**
		 * setter for pk column
		*/ 
		public function set pk(s_pk:String):void { 
			_pk = s_pk;
			_a_parameters[":pk"] = s_pk;
		}
		
		/**
		 * getter for pk column
		*/
		public function get pk():String {
			return _pk;
		}
		
		/**
		 * setter for id column
		*/ 
		public function set id(s_id:String):void { 
			_id = s_id;
			_a_parameters[":id"] = s_id;
		}
		
		/**
		 * getter for id column
		*/
		public function get id():String {
			return _id;
		}
		
		/**
		 * setter for description column
		*/ 
		public function set description(s_description:String):void { 
			_description = s_description;
			_a_parameters[":description"] = s_description;
		}
		
		/**
		 * getter for description column
		*/
		public function get description():String {
			return _description;
		}
		
		/**
		 * setter for unit_cost column
		*/ 
		public function set unit_cost(i_unit_cost:Number):void { 
			_unit_cost = i_unit_cost;
			_a_parameters[":unit_cost"] = i_unit_cost;
		}
		
		/**
		 * getter for unit_cost column
		*/
		public function get unit_cost(): Number {
			return _unit_cost;
		}
		
		/**
		 * setter for uom column
		*/ 
		public function set uom(s_uom:String):void { 
			_uom = s_uom;
			_a_parameters[":uom"] = s_uom;
		}
		
		/**
		 * getter for uom column
		*/
		public function get uom(): String {
			return _uom;
		}
		
		/**
		 * setter for lead_time column
		*/ 
		public function set lead_time(u_lead_time:uint):void { 
			_lead_time = u_lead_time;
			_a_parameters[":lead_time"] = u_lead_time;
		}
		
		/**
		 * getter for lead_time column
		*/
		public function get lead_time(): uint {
			return _lead_time;
		}
		
		/**
		 * setter for available_quantity column
		*/ 
		public function set available_quantity(u_available_quantity:int):void { 
			_available_quantity = u_available_quantity;
			_a_parameters[":available_quantity"] = u_available_quantity;
		}
		
		/**
		 * getter for available_quantity column
		*/
		public function get available_quantity(): int {
			return _available_quantity;
		}
		
		/**
		 * setter for reorder_quantity column
		*/ 
		public function set reorder_quantity(u_reorder_quantity:int):void { 
			_reorder_quantity = u_reorder_quantity;
			_a_parameters[":reorder_quantity"] = u_reorder_quantity;
		}
		
		/**
		 * getter for reorder_quantity column
		*/
		public function get reorder_quantity(): int {
			return _reorder_quantity;
		}
		
		/**
		 * setter for date_created column
		*/ 
		public function set date_created(s_date_created:String):void { 
			_date_created = s_date_created;
			_a_parameters[":date_created"] = s_date_created;
		}
		
		/**
		 * getter for date_created column
		*/
		public function get date_created():String {
			return _date_created;
		}
		
		/**
		 * setter for date_modified column
		*/ 
		public function set date_modified(s_date_modified:String):void { 
			_date_modified = s_date_modified;
			_a_parameters[":date_modified"] = s_date_modified;
		}
		
		/**
		 * getter for date_modified column
		*/
		public function get date_modified():String {
			return _date_modified;
		}
		
		/**
		 * setter for pk column
		*/ 
		public function set category_pk(s_category_pk:String):void { 
			_category_pk = s_category_pk;
			_a_parameters[":category_pk"] = s_category_pk;
		}
		
		/**
		 * getter for category_pk column
		*/
		public function get category_pk():String {
			return _category_pk;
		}
		
		/**
		 * sets s_columns
		*/
		private function func_add_column(str_value:String):void {
			if (_s_columns.length > 0) _s_columns += "," + str_value;
			else _s_columns = str_value;
		}
		
		/**
		 * getter for s_columns
		*/
		private function get s_columns():String {
			return _s_columns;
		}
		
		/**
		 * prepare strings for insert statements
		*/
		private function func_concatenate_insertion():void {
			_s_columns = _s_values = "";
			if (_pk != "") func_add_column("pk");
			
			if (_id != "") func_add_column("id");
			
			if (_description != "") func_add_column("description");
		
			if (_unit_cost > 0) func_add_column("unit_cost");
			
			if (_uom != "") func_add_column("uom");
			
			if (_lead_time >= 0) func_add_column("lead_time");
		
			if (_available_quantity >= 0) func_add_column("available_quantity");
			
			if (_reorder_quantity >= 0) func_add_column("reorder_quantity");
			
			if (_date_created != "") func_add_column("date_created");
			
			if (_date_modified != "") func_add_column("date_modified");
			
			if (_category_pk != "") func_add_column("category_pk");
			
			var pattern:RegExp = /,/gi;
			_s_values = _s_columns.replace(pattern, ",:");
			_s_values = ":" + _s_values;
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_insert_statement():String {
			func_concatenate_insertion();
			return "INSERT INTO products(" + _s_columns + ") VALUES(" + _s_values + ")";
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_delete_statement():String {
			_a_parameters = new Array();
			_a_parameters[":pk"] = this._pk;
			return "DELETE FROM products WHERE pk = :pk";
		}
		
		/**
		 * getter for s_update_statement column
		*/
		public function get s_update_statement():String {
			return "UPDATE products " + 
					"SET " + 
						"id = :id, description = :description, unit_cost = :unit_cost, " + 
						"uom = :uom, lead_time = :lead_time, " + 
						"available_quantity = :available_quantity, reorder_quantity = :reorder_quantity, " + 
						"date_modified = :date_modified, category_pk = :category_pk, date_created = :date_created " + 
					"WHERE pk = :pk";
		}
		
		/**
		 * getter for s_parameters
		*/
		public function func_set_sqlstatement(sql:SQLStatement, s_statement:String):void {
			sql.text = s_statement;
			for (var col:* in _a_parameters) {
				sql.parameters[col] = _a_parameters[col];
			}
			sql.execute();
		}
		
	}
}